import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
} from '@nestjs/common';
import { AddressService } from './address.service';
import { CreateAddressDto } from './dto/create-address.dto';
import { UpdateAddressDto } from './dto/update-address.dto';
import { RequireLogin, UserInfo } from 'src/custom.decorator';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';

@ApiTags('地址管理')
@ApiBearerAuth()
@Controller('address')
@RequireLogin()
export class AddressController {
  constructor(private readonly addressService: AddressService) {}

  @Post()
  @ApiOperation({ summary: '新增地址' })
  @ApiResponse({ status: 201, description: '新增成功' })
  async create(
    @Body() createAddressDto: CreateAddressDto,
    @UserInfo('userId') userId: number,
  ) {
    return await this.addressService.create(createAddressDto, userId);
  }

  @Get()
  @ApiOperation({ summary: '获取地址列表' })
  async findAll(@UserInfo('userId') userId: number) {
    return await this.addressService.findAll(userId);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取地址详情' })
  @ApiParam({ name: 'id', description: '地址ID' })
  async findOne(@Param('id') id: string, @UserInfo('userId') userId: number) {
    return await this.addressService.findOne(+id, userId);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新地址' })
  @ApiParam({ name: 'id', description: '地址ID' })
  @ApiResponse({ status: 200, description: '更新成功' })
  async update(
    @Param('id') id: string,
    @Body() updateAddressDto: UpdateAddressDto,
    @UserInfo('userId') userId: number,
  ) {
    return await this.addressService.update(+id, updateAddressDto, userId);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除地址' })
  @ApiParam({ name: 'id', description: '地址ID' })
  @ApiResponse({ status: 200, description: '删除成功' })
  async remove(@Param('id') id: string, @UserInfo('userId') userId: number) {
    return await this.addressService.remove(+id, userId);
  }
}
